﻿using System;
using System.Collections;
using System.Configuration.Install;
using System.Data;
using System.Data.SqlClient;
using System.ServiceProcess;
using System.Xml;
using System.Threading;
using System.Collections.Generic;

namespace DotNetAgent.Core
{
	public class WindowsServiceHelper
	{
		public static void InstallService(string Filename)
		{
			AssemblyInstaller Inst = null;
			Exception Ex;
			Hashtable SavedState = new Hashtable();
			try
			{
				Inst = new AssemblyInstaller(Filename, new string[0]);
				try
				{
					Inst.Install(SavedState);
				}
				catch (Exception exception1)
				{
					Ex = exception1;
					throw new AgentException("InstallService", "Error During AssemblyInstaller Installation", Ex);
				}
			}
			catch (Exception exception2)
			{
				Ex = exception2;
				throw new AgentException("InstallService", "Error During Installation", Ex);
			}
		}

		public static bool ServiceExists(string ServiceDisplayName)
		{
			foreach (ServiceController Svc in ServiceController.GetServices())
			{
				if (Svc.DisplayName == ServiceDisplayName)
				{
					return true;
				}
			}
			return false;
		}

		public static void StartAgentService(string AgentSrvcType)
		{
			ServiceBase AS = (ServiceBase)Activator.CreateInstance(Type.GetType(AgentSrvcType), (object[])null);
			ServiceBase.Run(new ServiceBase[] { AS });
		}

		public static int StartService(string Args)
		{
			int Res;
			string AgentName = "";
			try
			{
				FileLogConnector.DebugLog("Starting Service...");
				XmlDocument doc = new XmlDocument();
				XmlNode node = null;
				doc.LoadXml(Args);
				node = doc.DocumentElement.SelectSingleNode("Name");
				if (node == null)
				{
					throw new AgentException("No Name given");
				}
				AgentName = node.InnerText;
				if (!ServiceExists(AgentName))
				{
					return -1;
				}
				FileLogConnector.DebugLog("Instantiating Service Controller...");
				ServiceController Svc = new ServiceController(AgentName);
				if (Svc.Status != ServiceControllerStatus.Running)
				{
					FileLogConnector.DebugLog("Calling Start Service Method...(Start Arguments: " + Args + ")");
					Svc.Start(new string[] { Args });
					node = doc.DocumentElement.SelectSingleNode("Name");
					if (node == null)
					{
						throw new AgentException("No SecondsTimeout given");
					}
					Svc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, int.Parse(node.InnerText)));
					return ((Svc.Status == ServiceControllerStatus.Running) ? 2 : 1);
				}
				Res = 0;
			}
			catch (Exception Ex)
			{
				FileLogConnector.DebugLog("Error During Service Start (ServiceDisplayName: " + AgentName + ")", Ex);
				throw new AgentException("StartService", "Error During Service Start (ServiceDisplayName: " + AgentName + ")", Ex);
			}
			return Res;
		}

		public static int StopService(string ServiceDisplayName, int SecondsTimeout)
		{
			int Res;
			try
			{
				if (!ServiceExists(ServiceDisplayName))
				{
					return -1;
				}
				ServiceController Svc = new ServiceController(ServiceDisplayName);
				if (Svc.Status != ServiceControllerStatus.Stopped)
				{
					Svc.Stop();
					Svc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, SecondsTimeout));
					return ((Svc.Status == ServiceControllerStatus.Stopped) ? 2 : 1);
				}
				Res = 0;
			}
			catch (Exception Ex)
			{
				throw new AgentException("StartService", "Error During Service Stop", Ex);
			}
			return Res;
		}

		public static void UnInstallService(string Filename)
		{
			AssemblyInstaller Inst = null;
			Exception Ex;
			Hashtable SavedState = new Hashtable();
			try
			{
				Inst = new AssemblyInstaller(Filename, new string[0]);
				try
				{
					Inst.Uninstall(SavedState);
				}
				catch (Exception exception1)
				{
					Ex = exception1;
					throw new AgentException("UnInstallService", "Error During AssemblyInstaller UnInstallation", Ex);
				}
			}
			catch (Exception exception2)
			{
				Ex = exception2;
				throw new AgentException("UnInstallService", "Error During UnInstallation", Ex);
			}
		}
	}
}